<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Bitwise operation</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Bitwise_operation"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Bitwise_operation rootpage-Bitwise_operation skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Bitwise operation</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style>
<p>
In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>bitwise operation</b> operates on a <a href="Bit_string" class="mw-redirect" title="Bit string">bit string</a>, a <a href="Bit_array" title="Bit array">bit array</a> or a <a href="Binary_numeral" class="mw-redirect" title="Binary numeral">binary numeral</a> (considered as a bit string) at the level of its individual <a href="Bit" title="Bit">bits</a>. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the <a href="Central_processing_unit" title="Central processing unit">processor</a>. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
</p><p>On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer <a href="Instruction_pipeline" class="mw-redirect" title="Instruction pipeline">instruction pipelines</a> and other <a href="Computer_architecture" title="Computer architecture">architectural</a> design choices, bitwise operations do commonly use less power because of the reduced use of resources.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Bitwise_operators">Bitwise operators</h2></div>
<p>In the explanations below, any indication of a bit's position is counted from the right (least significant) side, advancing left. For example, the binary value 0001 (decimal 1) has zeroes at every position but the first (i.e., the rightmost) one.
</p>
<div class="mw-heading mw-heading3"><h3 id="NOT">NOT</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">See also: <a href="Ones'_complement" title="Ones' complement">Ones' complement</a></div>
<p>The <b>bitwise NOT</b>, or <b>bitwise complement</b>, is a <a href="Unary_operation" title="Unary operation">unary operation</a> that performs <a href="Logical_negation" class="mw-redirect" title="Logical negation">logical negation</a> on each bit, forming the <a href="Ones'_complement" title="Ones' complement">ones' complement</a> of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:
</p>
<pre>NOT <b>0</b>111 (decimal 7)
= <b>1</b>000 (decimal 8)
</pre>
<pre>NOT 10101011 (decimal 171)
= 01010100 (decimal 84)
</pre>
<p>The result is equal to the <a href="Two's_complement" title="Two's complement">two's complement</a> of the value minus one. If two's complement arithmetic is used, then <code>NOT x = -x − 1</code>.
</p><p>For unsigned <a href="Integer_(computer_science)" title="Integer (computer science)">integers</a>, the bitwise complement of a number is the "mirror reflection" of the number across the half-way point of the unsigned integer's range. For example, for 8-bit unsigned integers, <code>NOT x = 255 - x</code>, which can be visualized on a graph as a downward line that effectively "flips" an increasing range from 0 to 255, to a decreasing range from 255 to 0. A simple but illustrative example use is to invert a <a href="Grayscale_image" class="mw-redirect" title="Grayscale image">grayscale image</a> where each pixel is stored as an unsigned integer.
</p>
<div class="mw-heading mw-heading3"><h3 id="AND">AND</h3></div>
<p>A <b>bitwise AND</b> is a <a href="Binary_operation" title="Binary operation">binary operation</a> that takes two equal-length binary representations and performs the <a href="Logical_AND" class="mw-redirect" title="Logical AND">logical AND</a> operation on each pair of the corresponding bits. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:
</p>
<pre> 010<b>1</b> (decimal 5)
AND 001<b>1</b> (decimal 3)
= 000<b>1</b> (decimal 1)
</pre>
<p>The operation may be used to determine whether a particular bit is <i>set</i> (1) or <i>cleared</i> (0). For example, given a bit pattern 0011 (decimal 3), to determine whether the second bit is set we use a bitwise AND with a bit pattern containing 1 only in the second bit:
</p>
<pre> 00<b>1</b>1 (decimal 3)
AND 00<b>1</b>0 (decimal 2)
= 00<b>1</b>0 (decimal 2)
</pre>
<p>Because the result 0010 is non-zero, we know the second bit in the original pattern was set. This is often called <i>bit masking</i>. (By analogy, the use of <a href="Masking_tape" title="Masking tape">masking tape</a> covers, or <i>masks</i>, portions that should not be altered or portions that are not of interest. In this case, the 0 values mask the bits that are not of interest.)
</p><p>The bitwise AND may be used to clear selected bits (or <a href="Flag_word" class="mw-redirect" title="Flag word">flags</a>) of a <a href="Processor_register" title="Processor register">register</a> in which each bit represents an individual <a href="Boolean_data_type" title="Boolean data type">Boolean state</a>. This technique is an efficient way to store a number of Boolean values using as little memory as possible.
</p><p>For example, 0110 (decimal 6) can be considered a set of four flags numbered from right to left, where the first and fourth flags are clear (0), and the second and third flags are set (1). The third flag may be cleared by using a bitwise AND with the pattern that has a zero only in the third bit:
</p>
<pre> 0<b>1</b>10 (decimal 6)
AND 1<b>0</b>11 (decimal 11)
= 0<b>0</b>10 (decimal 2)
</pre>
<p>Because of this property, it becomes easy to check the <a href="Parity_(mathematics)" title="Parity (mathematics)">parity</a> of a binary number by checking the value of the lowest valued bit. Using the example above:
</p>
<pre> 0110 (decimal 6)
AND 0001 (decimal 1)
= 0000 (decimal 0)
</pre>
<p>Because 6 AND 1 is zero, 6 is divisible by two and therefore even.
</p>
<div class="mw-heading mw-heading3"><h3 id="OR">OR</h3></div>
<p>A <b>bitwise OR</b> is a <a href="Binary_operation" title="Binary operation">binary operation</a> that takes two bit patterns of equal length and performs the <a href="Logical_disjunction" title="Logical disjunction">logical inclusive OR</a> operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:
</p>
<pre> 0<b>101</b> (decimal 5)
OR 0<b>011</b> (decimal 3)
= 0<b>111</b> (decimal 7)
</pre>
<p>The bitwise OR may be used to set to 1 the selected bits of the register described above. For example, the fourth bit of 0010 (decimal 2) may be set by performing a bitwise OR with the pattern with only the fourth bit set:
</p>
<pre> <b>0</b>0<b>1</b>0 (decimal 2)
OR <b>1</b>0<b>0</b>0 (decimal 8)
= <b>1</b>0<b>1</b>0 (decimal 10)
</pre>
<div class="mw-heading mw-heading3"><h3 id="XOR">XOR</h3></div>
<p>A <b>bitwise XOR</b> is a <a href="Binary_operation" title="Binary operation">binary operation</a> that takes two bit patterns of equal length and performs the <a href="Exclusive_disjunction" class="mw-redirect" title="Exclusive disjunction">logical exclusive OR</a> operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:
</p>
<pre> 0<b>10</b>1 (decimal 5)
XOR 0<b>01</b>1 (decimal 3)
= 0<b>11</b>0 (decimal 6)
</pre>
<p>The bitwise XOR may be used to invert selected bits in a register (also called toggle or flip). Any bit may be toggled by XORing it with 1. For example, given the bit pattern 0010 (decimal 2) the second and fourth bits may be toggled by a bitwise XOR with a bit pattern containing 1 in the second and fourth positions:
</p>
<pre> <b>0</b>0<b>1</b>0 (decimal 2)
XOR <b>1</b>0<b>1</b>0 (decimal 10)
= <b>1</b>0<b>0</b>0 (decimal 8)
</pre>
<p>This technique may be used to manipulate bit patterns representing sets of Boolean states.
</p><p><a href="Assembly_language" title="Assembly language">Assembly language</a> programmers and optimizing <a href="Compiler" title="Compiler">compilers</a> sometimes use XOR as a short-cut to setting the value of a register to zero. Performing XOR on a value against itself always yields zero, and on many architectures this operation requires fewer clock cycles and less memory than loading a zero value and saving it to the register.
</p><p>If the set of bit strings of fixed length <i>n</i> (i.e. <a href="Word_(computer_architecture)" title="Word (computer architecture)">machine words</a>) is thought of as an <i>n</i>-dimensional <a href="Vector_space" title="Vector space">vector space</a> <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\bf {F}}_{2}^{n}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msubsup>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="bold">F</mi>
</mrow>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msubsup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\bf {F}}_{2}^{n}}</annotation>
</semantics>
</math></span><img src="./c04980a79072877cf9f9a06919ef85b2f8a410b9.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:2.901ex; height:2.843ex;" alt="{\displaystyle {\bf {F}}_{2}^{n}}" loading="lazy"></span> over the <a href="Field_(mathematics)" title="Field (mathematics)">field</a> <a href="GF(2)" title="GF(2)"><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\bf {F}}_{2}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="bold">F</mi>
</mrow>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\bf {F}}_{2}}</annotation>
</semantics>
</math></span><img src="./b98e58c96841ebd4455d27f4f234bc812c77bef9.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:2.737ex; height:2.509ex;" alt="{\displaystyle {\bf {F}}_{2}}" loading="lazy"></span></a>, then vector addition corresponds to the bitwise XOR.
</p>
<div class="mw-heading mw-heading3"><h3 id="Mathematical_equivalents">Mathematical equivalents</h3></div>
<p>Assuming <span class="nowrap"><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x\geq y}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
<mo>≥<!-- ≥ --></mo>
<mi>y</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x\geq y}</annotation>
</semantics>
</math></span><img src="./aded94d634c48071188bf96a76a4d3b7dfb28470.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:5.584ex; height:2.343ex;" alt="{\displaystyle x\geq y}" loading="lazy"></span></span>, for the non-negative integers, the bitwise operations can be written as follows:
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle {\begin{aligned}\operatorname {NOT} x&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}+1\right){\bmod {2}}\right]=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }\left[2^{\left\lfloor \log _{2}(x)\right\rfloor +1}-1-x\right]\\x\operatorname {AND} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\\x\operatorname {OR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)-\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right)\\x\operatorname {XOR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right]{\bmod {2}}\right)=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor +\left\lfloor {\frac {y}{2^{n}}}\right\rfloor \right){\bmod {2}}\right]\end{aligned}}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mrow class="MJX-TeXAtom-ORD">
<mtable columnalign="right left right left right left right left right left right left" rowspacing="3pt" columnspacing="0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em" displaystyle="true">
<mtr>
<mtd>
<mi>NOT</mi>
<mo><!-- --></mo>
<mi>x</mi>
</mtd>
<mtd>
<mi></mi>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
<mrow>
<mo>[</mo>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
<mo>+</mo>
<mn>1</mn>
</mrow>
<mo>)</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>]</mo>
</mrow>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<mrow>
<mo>[</mo>
<mrow>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mrow>
<mo>⌊</mo>
<mrow>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
</mrow>
<mo>⌋</mo>
</mrow>
<mo>+</mo>
<mn>1</mn>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo>−<!-- − --></mo>
<mi>x</mi>
</mrow>
<mo>]</mo>
</mrow>
</mtd>
</mtr>
<mtr>
<mtd>
<mi>x</mi>
<mi>AND</mi>
<mo><!-- --></mo>
<mi>y</mi>
</mtd>
<mtd>
<mi></mi>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>y</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
</mtd>
</mtr>
<mtr>
<mtd>
<mi>x</mi>
<mi>OR</mi>
<mo><!-- --></mo>
<mi>y</mi>
</mtd>
<mtd>
<mi></mi>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mo>+</mo>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>y</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mo>−<!-- − --></mo>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>y</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
</mtd>
</mtr>
<mtr>
<mtd>
<mi>x</mi>
<mi>XOR</mi>
<mo><!-- --></mo>
<mi>y</mi>
</mtd>
<mtd>
<mi></mi>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>[</mo>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mo>+</mo>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>y</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
</mrow>
<mo>]</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mo>=</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo fence="false" stretchy="false">⌊<!-- ⌊ --></mo>
<msub>
<mi>log</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mo><!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mo fence="false" stretchy="false">⌋<!-- ⌋ --></mo>
</mrow>
</munderover>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
<mrow>
<mo>[</mo>
<mrow>
<mrow>
<mo>(</mo>
<mrow>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>x</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
<mo>+</mo>
<mrow>
<mo>⌊</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>y</mi>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msup>
</mfrac>
</mrow>
<mo>⌋</mo>
</mrow>
</mrow>
<mo>)</mo>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mo lspace="thickmathspace" rspace="thickmathspace">mod</mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</mrow>
</mrow>
<mo>]</mo>
</mrow>
</mtd>
</mtr>
</mtable>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle {\begin{aligned}\operatorname {NOT} x&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}+1\right){\bmod {2}}\right]=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }\left[2^{\left\lfloor \log _{2}(x)\right\rfloor +1}-1-x\right]\\x\operatorname {AND} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\\x\operatorname {OR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)-\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right)\\x\operatorname {XOR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right]{\bmod {2}}\right)=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor +\left\lfloor {\frac {y}{2^{n}}}\right\rfloor \right){\bmod {2}}\right]\end{aligned}}}</annotation>
</semantics>
</math></span><img src="./a3f7b832b988987e1fea2dc5f3b22648a9298939.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -15.005ex; width:107.788ex; height:31.176ex;" alt="{\displaystyle {\begin{aligned}\operatorname {NOT} x&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}+1\right){\bmod {2}}\right]=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }\left[2^{\left\lfloor \log _{2}(x)\right\rfloor +1}-1-x\right]\\x\operatorname {AND} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\\x\operatorname {OR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)-\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right)\\x\operatorname {XOR} y&=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left(\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor {\bmod {2}}\right)+\left(\left\lfloor {\frac {y}{2^{n}}}\right\rfloor {\bmod {2}}\right)\right]{\bmod {2}}\right)=\sum _{n=0}^{\lfloor \log _{2}(x)\rfloor }2^{n}\left[\left(\left\lfloor {\frac {x}{2^{n}}}\right\rfloor +\left\lfloor {\frac {y}{2^{n}}}\right\rfloor \right){\bmod {2}}\right]\end{aligned}}}" loading="lazy"></span></dd></dl>
<div class="mw-heading mw-heading3"><h3 id="Truth_table_for_all_binary_logical_operators">Truth table for all binary logical operators</h3></div>
<p>There are 16 possible <a href="Truth_function" title="Truth function">truth functions</a> of two <a href="Binary_variable" class="mw-redirect" title="Binary variable">binary variables</a>; this defines a <a href="Truth_table" title="Truth table">truth table</a>.
</p><p>Here is the bitwise equivalent operations of two bits P and Q:
</p>
<table class="wikitable" style="margin:1em auto 1em auto; text-align:center;">
<tbody><tr>
<th><i>p</i></th>
<th><i>q</i>
</th>
<td rowspan="5" style="border-bottom:none">
</td>
<th><a href="Contradiction" title="Contradiction">F</a><sup>0</sup>
</th>
<th><a href="Logical_NOR" title="Logical NOR">NOR</a><sup>1</sup>
</th>
<th><a href="Converse_nonimplication" title="Converse nonimplication">Xq</a><sup>2</sup>
</th>
<th><a href="Negation" title="Negation"><b>¬p</b></a><sup>3</sup>
</th>
<th><a href="Material_nonimplication" title="Material nonimplication">↛</a><sup>4</sup>
</th>
<th><a href="Negation" title="Negation"><b>¬q</b></a><sup>5</sup>
</th>
<th><a href="XOR" class="mw-redirect" title="XOR">XOR</a><sup>6</sup>
</th>
<th><a href="Logical_NAND" class="mw-redirect" title="Logical NAND">NAND</a><sup>7</sup>
</th>
<td rowspan="5" style="border-bottom:none">
</td>
<th><a href="Logical_conjunction" title="Logical conjunction">AND</a><sup>8</sup>
</th>
<th><a href="Logical_biconditional" title="Logical biconditional">XNOR</a><sup>9</sup>
</th>
<th><a href="Projection_function" class="mw-redirect" title="Projection function">q</a><sup>10</sup>
</th>
<th><a href="Material_conditional" title="Material conditional">If/then</a><sup>11</sup>
</th>
<th><a href="Projection_function" class="mw-redirect" title="Projection function">p</a><sup>12</sup>
</th>
<th><a href="Converse_implication" class="mw-redirect" title="Converse implication">Then/if</a><sup>13</sup>
</th>
<th><a href="Logical_disjunction" title="Logical disjunction">OR</a><sup>14</sup>
</th>
<th><a href="Tautology_(logic)" title="Tautology (logic)">T</a><sup>15</sup>
</th></tr>
<tr>
<th>1</th>
<th>1
</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1
</td></tr>
<tr>
<th>1</th>
<th>0
</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1
</td></tr>
<tr>
<th>0</th>
<th>1
</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1
</td></tr>
<tr>
<th>0</th>
<th>0
</th>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1
</td></tr>
<tr>
<th colspan="2">Bitwise <br>equivalents
</th>
<td style="border-top:none">
</td>
<th><small>0</small>
</th>
<th><small>NOT <br>(p OR q)</small>
</th>
<th><small>(NOT p) <br>AND q</small>
</th>
<th><small>NOT <br>p</small>
</th>
<th><small>p AND <br>(NOT q)</small>
</th>
<th><small>NOT <br>q</small>
</th>
<th><small>p XOR q</small>
</th>
<th><small>NOT <br>(p AND q)</small>
</th>
<td style="border-top:none">
</td>
<th><small>p AND q</small>
</th>
<th><small>NOT <br>(p XOR q)</small>
</th>
<th><small>q</small>
</th>
<th><small>(NOT p) <br>OR q</small>
</th>
<th><small>p</small>
</th>
<th><small>p OR <br>(NOT q)</small>
</th>
<th><small>p OR q</small>
</th>
<th><small>1</small>
</th></tr></tbody></table>
<div class="mw-heading mw-heading2"><h2 id="Bit_shifts">Bit shifts</h2></div>
<div role="note" class="hatnote navigation-not-searchable">"Binary shift" redirects here. For the excess-3 code, see <a href="Shifted_binary_(code)" class="mw-redirect" title="Shifted binary (code)">Shifted binary (code)</a>. For the general concept, see <a href="Offset_binary" title="Offset binary">Offset binary</a>.</div>
<p>The <b>bit shifts</b> are sometimes considered bitwise operations, because they treat a value as a series of bits rather than as a numerical quantity. In these operations, the digits are moved, or <i>shifted</i>, to the left or right. <a href="Processor_register" title="Processor register">Registers</a> in a computer processor have a fixed width, so some bits will be "shifted out" of the register at one end, while the same number of bits are "shifted in" from the other end; the differences between bit shift operators lie in how they determine the values of the shifted-in bits.
</p>
<div class="mw-heading mw-heading3"><h3 id="Bit_addressing">Bit addressing</h3></div>
<p>If the width of the register (frequently 32 or even 64) is larger than the number of bits (usually 8) of the smallest addressable unit, frequently called byte, the shift operations induce an addressing scheme from the bytes to the bits.
Thereby the orientations "left" and "right" are taken from the standard writing of numbers in a <a href="Place-value_notation" class="mw-redirect" title="Place-value notation">place-value notation</a>, such that a left shift increases and a right shift decreases the value of the number ― if the left digits are read first, this makes up a <a href="Big-endian" class="mw-redirect" title="Big-endian">big-endian</a> orientation.
Disregarding the boundary effects at both ends of the register, arithmetic and logical shift operations behave the same, and a shift by 8 bit positions transports the bit pattern by 1 byte position in the following way:
</p>
<dl><dd><table>
<tbody><tr>
<td><a href="Little-endian" class="mw-redirect" title="Little-endian">Little-endian</a> ordering:
</td>
<td>a left shift by 8 positions increases the byte address by 1,
</td></tr>
<tr>
<td>
</td>
<td>a right shift by 8 positions decreases the byte address by 1.
</td></tr>
<tr>
<td><a href="Big-endian" class="mw-redirect" title="Big-endian">Big-endian</a> ordering:
</td>
<td>a left shift by 8 positions decreases the byte address by 1,
</td></tr>
<tr>
<td>
</td>
<td>a right shift by 8 positions increases the byte address by 1.
</td></tr></tbody></table></dd></dl>
<div class="mw-heading mw-heading3"><h3 id="Arithmetic_shift">Arithmetic shift</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Arithmetic_shift" title="Arithmetic shift">Arithmetic shift</a></div>
<p>In an <i>arithmetic shift</i> (sticky shift), the bits that are shifted out of either end are discarded. In a left arithmetic shift, zeros are shifted in on the right; in a right arithmetic shift, the <a href="Sign_bit" title="Sign bit">sign bit</a> (the MSB in two's complement) is shifted in on the left, thus preserving the sign of the operand.
</p><p>This example uses an 8-bit register, interpreted as two's complement:
</p>
<pre> 00010111 (decimal +23) LEFT-SHIFT
= 0010111<b>0</b> (decimal +46)
</pre>
<pre> 10010111 (decimal −105) RIGHT-SHIFT
= <b>1</b>1001011 (decimal −53)
</pre>
<p>In the first case, the leftmost digit was shifted past the end of the register, and a new 0 was shifted into the rightmost position. In the second case, the rightmost 1 was shifted out (perhaps into the <a href="Carry_flag" title="Carry flag">carry flag</a>), and a new 1 was copied into the leftmost position, preserving the sign of the number. Multiple shifts are sometimes shortened to a single shift by some number of digits. For example:
</p>
<pre> 00010111 (decimal +23) LEFT-SHIFT-BY-TWO
= 010111<b>00</b> (decimal +92)
</pre>
<p>A left arithmetic shift by <i>n</i> is equivalent to multiplying by 2<sup><i>n</i></sup> (provided the value does not <a href="Arithmetic_overflow" class="mw-redirect" title="Arithmetic overflow">overflow</a>), while a right arithmetic shift by <i>n</i> of a <a href="Two's_complement" title="Two's complement">two's complement</a> value is equivalent to taking the <a href="Floor_and_ceiling_functions" title="Floor and ceiling functions">floor</a> of division by 2<sup><i>n</i></sup>. If the binary number is treated as <a href="Ones'_complement" title="Ones' complement">ones' complement</a>, then the same right-shift operation results in division by 2<sup><i>n</i></sup> and <a href="Rounding#Round_half_towards_zero" title="Rounding">rounding toward zero</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Logical_shift">Logical shift</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Logical_shift" title="Logical shift">Logical shift</a></div>
<table style="float:right;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
</td>
<td>
</td></tr></tbody></table>
<p>In a <i>logical shift</i> (zero fill shift), zeros are shifted in to replace the discarded bits. Therefore, the logical and arithmetic left-shifts are exactly the same.
</p><p>However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed <a href="Two's_complement" title="Two's complement">two's complement</a> binary numbers.
</p>
<div style="clear:both;" class=""></div>
<div class="mw-heading mw-heading3"><h3 id="Circular_shift">Circular shift</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Further information: <a href="Circular_shift" title="Circular shift">Circular shift</a></div>
<p>
Another form of shift is the <i>circular shift</i>, <i>bitwise rotation</i> or <i>bit rotation</i>.
</p>
<div class="mw-heading mw-heading4"><h4 id="Rotate">Rotate</h4></div>
<table style="float:right;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
</td>
<td>
</td></tr></tbody></table>
<p>In this operation, sometimes called <i>rotate no carry</i>, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted into the right during a left-shift is whatever value was shifted out on the left, and vice versa for a right-shift operation. This is useful if it is necessary to retain all the existing bits, and is frequently used in digital <a href="Cryptography" title="Cryptography">cryptography</a>.
</p>
<div style="clear:both;" class=""></div>
<div class="mw-heading mw-heading4"><h4 id="Rotate_through_carry">Rotate through carry</h4></div>
<table style="float:right;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
</td>
<td>
</td></tr></tbody></table>
<p><i>Rotate through carry</i> is a variant of the rotate operation, where the bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag.
</p><p>A single <i>rotate through carry</i> can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then <code>x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE</code> is a logical right-shift, and if the carry flag contains a copy of the sign bit, then <code>x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE</code> is an arithmetic right-shift. For this reason, some microcontrollers such as low end <a href="PIC_microcontroller" class="mw-redirect" title="PIC microcontroller">PICs</a> just have <i>rotate</i> and <i>rotate through carry</i>, and don't bother with arithmetic or logical shift instructions.
</p><p>Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native <a href="Word_size" class="mw-redirect" title="Word size">word size</a>, because if a large number is stored in two registers, the bit that is shifted off one end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
</p>
<div style="clear:both;" class=""></div>
<div class="mw-heading mw-heading3"><h3 id="In_high-level_languages">In high-level languages</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Further information: <a href="Circular_shift#Implementing_circular_shifts" title="Circular shift">Circular shift § Implementing circular shifts</a></div>
<div class="mw-heading mw-heading4"><h4 id="In_C_family_of_languages">In C family of languages</h4></div>
<p>
In C and C++ languages, the logical shift operators are "<code><<</code>" for left shift and "<code>>></code>" for right shift. The number of places to shift is given as the second argument to the operator. For example,
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
</pre></div>
<p>assigns <code>x</code> the result of shifting <code>y</code> to the left by two bits, which is equivalent to a multiplication by four.
</p><p>Shifts can result in implementation-defined behavior or <a href="Undefined_behavior" title="Undefined behavior">undefined behavior</a>, so care must be taken when using them. The result of shifting by a bit count greater than or equal to the word's size is undefined behavior in C and C++.<sup id="cite_ref-:0_2-0" class="reference"><a href="#cite_note-:0-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> Right-shifting a negative value is implementation-defined and not recommended by good coding practice;<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> the result of left-shifting a signed value is undefined if the result cannot be represented in the result type.<sup id="cite_ref-:0_2-1" class="reference"><a href="#cite_note-:0-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p>In C#, the right-shift is an arithmetic shift when the first operand is an int or long. If the first operand is of type uint or ulong, the right-shift is a logical shift.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading5"><h5 id="Circular_shifts">Circular shifts</h5></div>
<p>
The C-family of languages lack a rotate operator (although C++20 provides <code>std::rotl</code> and <code>std::rotr</code>), but one can be synthesized from the shift operators. Care must be taken to ensure the statement is well formed to avoid <a href="Undefined_behavior" title="Undefined behavior">undefined behavior</a> and <a href="Timing_attack" title="Timing attack">timing attacks</a> in software with security requirements.<sup id="cite_ref-StackOverflow_6-0" class="reference"><a href="#cite_note-StackOverflow-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> For example, a naive implementation that left-rotates a 32-bit unsigned value <code>x</code> by <code>n</code> positions is simply
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">uint32_t</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...;</span>
<span class="kt">uint32_t</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="p">(</span><span class="mi">32</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">n</span><span class="p">));</span>
</pre></div>
<p>However, a shift by <code>0</code> bits results in undefined behavior in the right-hand expression <code>(x >> (32 - n))</code> because <code>32 - 0</code> is <code>32</code>, and <code>32</code> is outside the range 0–31 inclusive. A second try might result in
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">uint32_t</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...;</span>
<span class="kt">uint32_t</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="p">(</span><span class="mi">32</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">n</span><span class="p">))</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
</pre></div>
<p>where the shift amount is tested to ensure that it does not introduce undefined behavior. However, the branch adds an additional code path and presents an opportunity for timing analysis and attack, which is often not acceptable in high-integrity software.<sup id="cite_ref-StackOverflow_6-1" class="reference"><a href="#cite_note-StackOverflow-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> In addition, the code compiles to multiple machine instructions, which is often less efficient than the processor's native instruction.
</p><p>To avoid the undefined behavior and branches under <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a> and <a href="Clang" title="Clang">Clang</a>, the following is recommended. The pattern is recognized by many compilers, and the compiler will emit a single rotate instruction:<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-LLVM_9-0" class="reference"><a href="#cite_note-LLVM-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">uint32_t</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">...;</span>
<span class="kt">uint32_t</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="p">(</span><span class="o">-</span><span class="n">n</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="mi">31</span><span class="p">));</span>
</pre></div>
<p>There are also compiler-specific <a href="Intrinsic_function" title="Intrinsic function">intrinsics</a> implementing <a href="Circular_shift" title="Circular shift">circular shifts</a>, like <a rel="nofollow" class="external text" href="http://msdn.microsoft.com/en-us/library/t5e2f3sc(VS.80).aspx">_rotl8, _rotl16</a>, <a rel="nofollow" class="external text" href="http://msdn.microsoft.com/en-us/library/yy0728bz(VS.80).aspx">_rotr8, _rotr16</a> in Microsoft <a href="Visual_C%2B%2B" class="mw-redirect" title="Visual C++">Visual C++</a>. Clang provides some rotate intrinsics for Microsoft compatibility that suffers the problems above.<sup id="cite_ref-LLVM_9-1" class="reference"><a href="#cite_note-LLVM-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> GCC does not offer rotate intrinsics. Intel also provides x86 <a rel="nofollow" class="external text" href="https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=rot&techs=Other">intrinsics</a>.
</p>
<div class="mw-heading mw-heading4"><h4 id="Java">Java</h4></div>
<p>
In <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, all integer types are signed, so the "<code><<</code>" and "<code>>></code>" operators perform arithmetic shifts. Java adds the operator "<code>>>></code>" to perform logical right shifts, but since the logical and arithmetic left-shift operations are identical for signed integer, there is no "<code><<<</code>" operator in Java.
</p><p>More details of Java shift operators:<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li>The operators <code><<</code> (left shift), <code>>></code> (signed right shift), and <code>>>></code> (unsigned right shift) are called the <i>shift operators</i>.</li>
<li>The type of the shift expression is the promoted type of the left-hand operand. For example, <code>aByte >>> 2</code> is equivalent to <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" style="" dir="ltr"><span class="p">((</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="n">aByte</span><span class="p">)</span><span class="w"> </span><span class="o">>>></span><span class="w"> </span><span class="mi">2</span></code>.</li>
<li>If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x1f (0b11111).<sup id="cite_ref-jls15.22.1_11-0" class="reference"><a href="#cite_note-jls15.22.1-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> The shift distance actually used is therefore always in the range 0 to 31, inclusive.</li>
<li>If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & with the mask value 0x3f (0b111111).<sup id="cite_ref-jls15.22.1_11-1" class="reference"><a href="#cite_note-jls15.22.1-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> The shift distance actually used is therefore always in the range 0 to 63, inclusive.</li>
<li>The value of <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">n >>> s</code> is <i>n</i> right-shifted <i>s</i> bit positions with zero-extension.</li>
<li>In bit and shift operations, the type <code><i>byte</i></code> is implicitly converted to <code><i>int</i></code>. If the byte value is negative, the highest bit is one, then ones are used to fill up the extra bytes in the int. So <code class="mw-highlight mw-highlight-lang-java mw-content-ltr" style="" dir="ltr"><span class="kt">byte</span><span class="w"> </span><span class="n">b1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b1</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mh">0x0200</span><span class="p">;</span></code> will result in <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">i == -5</code>.</li></ul>
<div class="mw-heading mw-heading4"><h4 id="JavaScript">JavaScript</h4></div>
<p>
<a href="JavaScript" title="JavaScript">JavaScript</a> uses bitwise operations to evaluate each of two or more <a href="Units_place" class="mw-redirect" title="Units place">units place</a> to 1 or 0.<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading4"><h4 id="Pascal">Pascal</h4></div>
<p>
In Pascal, as well as in all its dialects (such as <a href="Object_Pascal" title="Object Pascal">Object Pascal</a> and <a href="GNU_Pascal" title="GNU Pascal">Standard Pascal</a>), the logical left and right shift operators are "<code>shl</code>" and "<code>shr</code>", respectively. Even for signed integers, <code>shr</code> behaves like a logical shift, and does not copy the sign bit. The number of places to shift is given as the second argument. For example, the following assigns <i>x</i> the result of shifting <i>y</i> to the left by two bits:
</p>
<div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="n">x</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="k">shl</span><span class="w"> </span><span class="mi">2</span><span class="o">;</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Other">Other</h2></div>
<ul><li><a href="Popcount" class="mw-redirect" title="Popcount">popcount</a>, used in cryptography</li>
<li><a href="Count_leading_zeros" class="mw-redirect" title="Count leading zeros">count leading zeros</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Applications">Applications</h2></div>
<p>Bitwise operations are necessary particularly in lower-level programming such as device drivers, low-level graphics, communications protocol packet assembly, and decoding.
</p><p>Although machines often have efficient built-in instructions for performing arithmetic and logical operations, all these operations can be performed by combining the bitwise operators and zero-testing in various ways.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> For example, here is a <a href="Pseudocode" title="Pseudocode">pseudocode</a> implementation of <a href="Ancient_Egyptian_multiplication" title="Ancient Egyptian multiplication">ancient Egyptian multiplication</a> showing how to multiply two arbitrary integers <code>a</code> and <code>b</code> (<code>a</code> greater than <code>b</code>) using only bitshifts and addition:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">c</span><span class="w"> </span><span class="err">←</span><span class="w"> </span><span class="mi">0</span>
<span class="k">while</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="err">≠</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">b</span><span class="w"> </span><span class="n">and</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="err">≠</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="err">←</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">a</span>
<span class="w"> </span><span class="n">left</span><span class="w"> </span><span class="n">shift</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="n">right</span><span class="w"> </span><span class="n">shift</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="mi">1</span>
<span class="k">return</span><span class="w"> </span><span class="n">c</span>
</pre></div>
<p>Another example is a pseudocode implementation of addition, showing how to calculate a sum of two integers <code>a</code> and <code>b</code> using bitwise operators and zero-testing:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">while</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="err">≠</span><span class="w"> </span><span class="mi">0</span>
<span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="err">←</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">and</span><span class="w"> </span><span class="n">a</span>
<span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="err">←</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">xor</span><span class="w"> </span><span class="n">a</span>
<span class="w"> </span><span class="n">left</span><span class="w"> </span><span class="n">shift</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="mi">1</span>
<span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="err">←</span><span class="w"> </span><span class="n">c</span>
<span class="k">return</span><span class="w"> </span><span class="n">b</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Boolean_algebra">Boolean algebra</h2></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Boolean_algebra" title="Boolean algebra">Boolean algebra</a></div>
<p>Sometimes it is useful to simplify complex expressions made up of bitwise operations, for example when writing compilers. The goal of a compiler is to translate a <a href="High-level_programming_language" title="High-level programming language">high-level programming language</a> into the most efficient <a href="Machine_code" title="Machine code">machine code</a> possible. Boolean algebra is used to simplify complex bitwise expressions.
</p>
<div class="mw-heading mw-heading3"><h3 id="AND_2">AND</h3></div>
<ul><li><code>x & y = y & x</code></li>
<li><code>x & (y & z) = (x & y) & z</code></li>
<li><code>x & 0xFFFF = x</code><sup id="cite_ref-:1_14-0" class="reference"><a href="#cite_note-:1-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup></li>
<li><code>x & 0 = 0</code></li></ul>
<ul><li><code>x & x = x</code></li></ul>
<div class="mw-heading mw-heading3"><h3 id="OR_2">OR</h3></div>
<ul><li><code>x | y = y | x</code></li>
<li><code>x | (y | z) = (x | y) | z</code></li>
<li><code>x | 0 = x</code></li>
<li><code>x | 0xFFFF = 0xFFFF</code></li>
<li><code>x | x = x</code></li></ul>
<div class="mw-heading mw-heading3"><h3 id="NOT_2">NOT</h3></div>
<ul><li><code>~(~x) = x</code></li></ul>
<div class="mw-heading mw-heading3"><h3 id="XOR_2">XOR</h3></div>
<ul><li><code>x ^ y = y ^ x</code></li>
<li><code>x ^ (y ^ z) = (x ^ y) ^ z</code></li>
<li><code>x ^ 0 = x</code></li>
<li><code>x ^ y ^ y = x</code></li>
<li><code>x ^ x = 0</code></li>
<li><code>x ^ 0xFFFF = ~x</code></li></ul>
<p>Additionally, XOR can be composed using the 3 basic operations (AND, OR, NOT)
</p>
<ul><li><code>a ^ b = (a | b) & (~a | ~b)</code></li>
<li><code>a ^ b = (a & ~b) | (~a & b)</code></li></ul>
<div class="mw-heading mw-heading3"><h3 id="Others">Others</h3></div>
<ul><li><code>x | (x & y) = x</code></li>
<li><code>x & (x | y) = x</code></li>
<li><code>~(x | y) = ~x & ~y</code></li>
<li><code>~(x & y) = ~x | ~y</code></li>
<li><code>x | (y & z) = (x | y) & (x | z)</code></li>
<li><code>x & (y | z) = (x & y) | (x & z)</code></li>
<li><code>x & (y ^ z) = (x & y) ^ (x & z)</code></li>
<li><code>x + y = (x ^ y) + ((x & y) << 1)</code></li>
<li><code>x - y = ~(~x + y)</code></li></ul>
<div class="mw-heading mw-heading3"><h3 id="Inverses_and_solving_equations">Inverses and solving equations</h3></div>
<p>It can be hard to solve for variables in Boolean algebra, because unlike regular algebra, several operations do not have inverses. Operations without inverses lose some of the original data bits when they are performed, and it is not possible to recover this missing information.
</p>
<ul><li>Has inverse
<ul><li>NOT</li>
<li>XOR</li>
<li>Rotate left</li>
<li>Rotate right</li></ul></li>
<li>No inverse
<ul><li>AND</li>
<li>OR</li>
<li>Shift left</li>
<li>Shift right</li></ul></li></ul>
<div class="mw-heading mw-heading3"><h3 id="Order_of_operations">Order of operations</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Operators_in_C_and_C%2B%2B#Operator_precedence" title="Operators in C and C++">Operators in C and C++ § Operator precedence</a></div>
<p>Operations at the top of this list are executed first. See the main article for a more complete list.
</p>
<ul><li><code>( )</code></li>
<li><code>~ -</code><sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup></li>
<li><code>* / %</code></li>
<li><code>+ -</code><sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup></li>
<li><code><< >></code></li>
<li><code>&</code></li>
<li><code>^</code></li>
<li><code>|</code></li></ul>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1184024115">
/* start https://en.wikipedia.org/ */
.mw-parser-output .div-col{margin-top:0.3em;column-width:30em}.mw-parser-output .div-col-small{font-size:90%}.mw-parser-output .div-col-rules{column-rule:1px solid #aaa}.mw-parser-output .div-col dl,.mw-parser-output .div-col ol,.mw-parser-output .div-col ul{margin-top:0}.mw-parser-output .div-col li,.mw-parser-output .div-col dd{page-break-inside:avoid;break-inside:avoid-column}
/* end https://en.wikipedia.org/ */
</style><div class="div-col" style="column-width: 22em;">
<ul><li><a href="Arithmetic_logic_unit" title="Arithmetic logic unit">Arithmetic logic unit</a></li>
<li><a href="Bit_manipulation" title="Bit manipulation">Bit manipulation</a></li>
<li><a href="Bitboard" title="Bitboard">Bitboard</a></li>
<li><a href="Bitwise_operations_in_C" title="Bitwise operations in C">Bitwise operations in C</a></li>
<li><a href="Double_dabble" title="Double dabble">Double dabble</a></li>
<li><a href="Find_first_set" title="Find first set">Find first set</a></li>
<li><a href="Karnaugh_map" title="Karnaugh map">Karnaugh map</a></li>
<li><a href="Logic_gate" title="Logic gate">Logic gate</a></li>
<li><a href="Logical_operator" class="mw-redirect" title="Logical operator">Logical operator</a></li>
<li><a href="Primitive_data_type" title="Primitive data type">Primitive data type</a></li></ul>
</div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://cmicrotek.com/wordpress_159256135/">"CMicrotek Low-power Design Blog"</a>. CMicrotek<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-08-12</span></span>.</cite></span>
</li>
<li id="cite_note-:0-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-:0_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-:0_2-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm">JTC1/SC22/WG14 N843 "C programming language"</a>, section 6.5.7</span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://en.cppreference.com/w/cpp/language/operator_arithmetic#Bitwise_shift_operators">"Arithmetic operators - cppreference.com"</a>. <i>en.cppreference.com</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2016-07-06</span></span>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.securecoding.cert.org/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands">"INT13-C. Use bitwise operators only on unsigned operands"</a>. <i>CERT: Secure Coding Standards</i>. Software Engineering Institute, Carnegie Mellon University<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-09-07</span></span>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://msdn.microsoft.com/en-us/library/xt18et0d%28v=vs.110%29.aspx">"Operator (C# Reference)"</a>. Microsoft<span class="reference-accessdate">. Retrieved <span class="nowrap">2013-07-14</span></span>.</cite></span>
</li>
<li id="cite_note-StackOverflow-6"><span class="mw-cite-backlink">^ <a href="#cite_ref-StackOverflow_6-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-StackOverflow_6-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://stackoverflow.com/q/31387778">"Near constant time rotate that does not violate the standards?"</a>. Stack Exchange Network<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-08-12</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157">"Poor optimization of portable rotate idiom"</a>. GNU GCC Project<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-08-11</span></span>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://software.intel.com/en-us/forums/topic/580884">"Circular rotate that does not violate C/C++ standard?"</a>. Intel Developer Forums<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-08-12</span></span>.</cite></span>
</li>
<li id="cite_note-LLVM-9"><span class="mw-cite-backlink">^ <a href="#cite_ref-LLVM_9-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-LLVM_9-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://llvm.org/bugs/show_bug.cgi?id=24226">"Constant not propagated into inline assembly, results in "constraint 'I' expects an integer constant expression""</a>. LLVM Project<span class="reference-accessdate">. Retrieved <span class="nowrap">2015-08-11</span></span>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text">The Java Language Specification, section <a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19">15.19. Shift Operators</a></span>
</li>
<li id="cite_note-jls15.22.1-11"><span class="mw-cite-backlink">^ <a href="#cite_ref-jls15.22.1_11-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-jls15.22.1_11-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.1">"Chapter 15. Expressions"</a>. <i>oracle.com</i>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://www.w3schools.com/js/js_bitwise.asp">"JavaScript Bitwise"</a>. <i>W3Schools.com</i>.</span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://bisqwit.iki.fi/story/howto/bitmath/">"Synthesizing arithmetic operations using bit-shifting tricks"</a>. Bisqwit.iki.fi. 2014-02-15<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-03-08</span></span>.</cite></span>
</li>
<li id="cite_note-:1-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-:1_14-0">^</a></b></span> <span class="reference-text">Throughout this article, 0xFFFF means that all the bits in your data type need to be set to 1. The exact number of bits depends on the width of the data type.</span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text">- is negation here, not subtraction</span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text">- is subtraction here, not negation</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://www.miniwebtool.com/bitwise-calculator/">Online Bitwise Calculator</a> supports Bitwise AND, OR and XOR</li>
<li><a rel="nofollow" class="external text" href="https://gitlab.com/viablu/xorcat">XORcat</a>, a tool for bitwise-XOR files/streams</li>
<li><a rel="nofollow" class="external text" href="http://www.cs.uiowa.edu/~jones/bcd/divide.html">Division using bitshifts</a></li>
<li>"<a rel="nofollow" class="external text" href="https://web.archive.org/web/20150125062918/http://demonstrations.wolfram.com/BitwiseOperationsModN/">Bitwise Operations Mod N</a>" by Enrique Zeleny, <a href="Wolfram_Demonstrations_Project" title="Wolfram Demonstrations Project">Wolfram Demonstrations Project</a>.</li>
<li>"<a rel="nofollow" class="external text" href="http://demonstrations.wolfram.com/PlotsOfCompositionsOfBitwiseOperations/">Plots Of Compositions Of Bitwise Operations</a>" by Enrique Zeleny, The Wolfram Demonstrations Project.</li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-06-16" href="https://en.wikipedia.org/wiki/?title=Bitwise_operation&oldid=1295915012">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>